<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/30
 * Time: 14:28
 */

namespace app\api\controller\Index;

use  app\Request;
use app\api\controller\AppController;
use app\Service\SignatureService;
use Qcloud\Cos\Client as CosClient;
use app\Utils\Util;
use think\File;

class Uploads extends AppController
{
    /**
     * 视频封面上传
     */
    public function uploads(Request $request)
    {
        $file = $request->file('file');
        $type = (int)$request->post('type', 1);
        //判断是否为空
        if (empty($file)) {
            return Util::returnJson(self::CODE_ERROR, null, '请上传file图片');
        }
        $realPath = $file->getRealPath();   //临时文件的绝对路径
        //上传路径
        if ($type == 1) {
            $key = config('app.images') . md5(time()) . '.png';
        } else {
            $key = config('app.video') . md5(time()) . '.mp4';
        }
        $this->cosClient()->upload(env('COS.BUCKET'), $key, fopen($realPath, 'rb'));
        $url = env('COS.HOST') . $key;
        return Util::returnJson(self::CODE_SUCCESS, $url, '上传成功');
    }

    /**
     * @return CosClient
     */
    private function cosClient()
    {
        $cosClient = new CosClient(config('app.cos'));
        return $cosClient;
    }

    /**
     * @param SignatureService $service
     * @return array
     * 腾讯云签名
     */
    public function signature(SignatureService $service)
    {

        $signature = $service->signature();
        return Util::returnJson(self::CODE_SUCCESS, ['signature' => $signature], '获取成功');
    }

}